package common.utils;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Map;

public class MyMapUtils extends MapUtils{

    static Logger logger= LoggerFactory.getLogger(MapUtils.class);

    /**
     * 将javabean实体类转为map类型，然后返回一个map类型的值
     *
     * @param obj
     * @return
     */
    public static Map beanToMap(Object obj, Boolean isSetVal) {
        Map addMap = new HashedMap();
        try {
            BeanInfo bi= Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] descriptors =bi.getPropertyDescriptors();
            PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
            for (int i = 0; i < descriptors.length; i++) {
                String name = descriptors[i].getName();
                if (!"class".equals(name)) {
                    if (!StringUtils.isEmpty(propertyUtilsBean
                            .getNestedProperty(obj, name))) {
                        addMap.put(name, propertyUtilsBean.getNestedProperty(obj, name));
                    }else {
                        if(isSetVal){
                            Class<?> type = propertyUtilsBean.getPropertyType(obj, name);
                            addMap.put(name, setPropertyVal(type));
                        }
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return addMap;
    }

    /**
     *  设置属性值
     *
     * @param type
     * @return
     */
    private static Object setPropertyVal(Class<?> type) {
        if (type.getName().equalsIgnoreCase("long")||type.getName().equalsIgnoreCase("int")) {
            return 0;
        }
        return null;
    }

    /**
     * 获取更新数据map
     *
     * @param obj
     * @return
     */
    public static Map getUpdateMap(Object obj) {
        Map map = new HashedMap();
        try {
            PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
            BeanInfo bi= Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] descriptors =bi.getPropertyDescriptors();
            for (int i = 0; i < descriptors.length; i++) {
                String name = descriptors[i].getName();
                if (!"class".equals(name)) {
                    if (!StringUtils.isEmpty(propertyUtilsBean
                            .getNestedProperty(obj, name))) {
                        map.put(name, propertyUtilsBean.getNestedProperty(obj, name));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return map;
    }

    public static <T> T mapToBean(T t, Map<Object, String> map) {
        try {
            // 获取javaBean属性
            BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
            // 创建 JavaBean 对象
            Object bean = t.getClass().newInstance();
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            if (propertyDescriptors != null && propertyDescriptors.length > 0) {
                String propertyName = null; // javaBean属性名
                Object propertyValue = null; // javaBean属性值
                for (PropertyDescriptor pd : propertyDescriptors) {
                    propertyName = pd.getName();
                    if (map.containsKey(propertyName)) {
                        propertyValue = map.get(propertyName);
                        pd.getWriteMethod().invoke(bean, new Object[]{propertyValue});
                    }
                }
                return (T) bean;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}